﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Framework.Core.Condition
{
    public class ComparsionOperation<T> : IOperation where T : IComparable
    {
        private ComparsionType _Type;

        public ComparsionType Type
        {
            get
            {
                return _Type;
            }
            set
            {
                if (this._Type != value)
                    this._Type = value;
            }
        }

        public ComparsionOperation(ComparsionType type)
        {
            this._Type = type;
        }

        bool IOperation.Calculate(IExpression leftExpression, IExpression rightExpression)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public bool Calculate(IExpressionItem leftExpressionItem, IExpressionItem rightExpressionItem)
        {
            if (leftExpressionItem == null || rightExpressionItem == null)
            {
                return false;
            }

            ComparsionExpressionItem<T> ei1 = (ComparsionExpressionItem<T>)leftExpressionItem;
            ComparsionExpressionItem<T> ei2 = (ComparsionExpressionItem<T>)rightExpressionItem;
            switch (this.Type)
            {
                case ComparsionType.Equal:
                    return ei1.Item.CompareTo(ei2.Item) == 0 ? true : false;
                case ComparsionType.NotEqual:
                    return ei1.Item.CompareTo(ei2.Item) != 0 ? true : false;
                case ComparsionType.Greater:
                    return ei1.Item.CompareTo(ei2.Item) > 0 ? true : false;
                case ComparsionType.GreaterOrEqual:
                    return ei1.Item.CompareTo(ei2.Item) >= 0 ? true : false;
                case ComparsionType.Lesser:
                    return ei1.Item.CompareTo(ei2.Item) < 0 ? true : false;
                case ComparsionType.LesserOrEqual:
                    return ei1.Item.CompareTo(ei2.Item) <= 0 ? true : false;
                default:
                    return false;
            }
        }

        bool IOperation.Calculate(IExpression leftExpression, IExpressionItem rightExpressionItem)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        bool IOperation.Calculate(IExpressionItem leftExpressionItem, IExpression rightExpression)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}
